package com.hdjk.view.activity.personalcenter;

import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hdjk.view.R;
import com.hdjk.view.adapter.RecommendListAdapter;
import com.hdjk.view.callback.CallBackListener;
import com.hdjk.view.entity.UserEntity;
import com.hdjk.view.network.PersonalNetWork;
import com.hdjk.view.util.UserTokenUtil;
import com.lst.projectlib.base.BaseActivity;
import com.lst.projectlib.entity.Msg;

import java.util.List;

/**
 * Created by sxy on 2017/11/15.
 */

public class RecommendActivity extends BaseActivity implements View.OnClickListener {
    private List<UserEntity> mList;
    private RecommendListAdapter mAdapter;

    // Content View Elements
    private RelativeLayout rel_bg;
    private ImageView iv_back;
    private TextView tv_number;
    private TextView tv_count;
    private TextView tv_share;
    private ListView lv_data_list;
    // End Of Content View Elements

    private void bindViews() {
        rel_bg = (RelativeLayout) findViewById(R.id.rel_bg);
        iv_back = (ImageView) findViewById(R.id.iv_back);
        tv_share = (TextView) findViewById(R.id.tv_share);
        tv_number = (TextView) findViewById(R.id.tv_number);
        tv_count = (TextView) findViewById(R.id.tv_count);
        lv_data_list = (ListView) findViewById(R.id.lv_data_list);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_recommend);
        bindViews();
        iv_back.setOnClickListener(this);
        tv_share.setOnClickListener(this);
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        ViewGroup.LayoutParams layoutParams=rel_bg.getLayoutParams();
        layoutParams.width=dm.widthPixels;
        layoutParams.height=(int)((float)(dm.widthPixels)/1080*890);
        rel_bg.setLayoutParams(layoutParams);
    }

    @Override
    protected void initData() {

        getUserId();
        getRecommendList();
    }

    private void getUserId(){
        PersonalNetWork.getUserId(UserTokenUtil.getToken(RecommendActivity.this), new CallBackListener() {
            @Override
            public void onCallBackCompleted(Msg msg) {
                if (msg != null && msg.getSuccess() && msg.getData() != null) {
                    tv_number.setText("推荐码："+msg.getData());
                }
            }

            @Override
            public void onCallBackError(Msg msg) {

            }
        });
    }

    private void getRecommendList() {
        mDialog.show();
        PersonalNetWork.getRecommendList(UserTokenUtil.getToken(RecommendActivity.this), new CallBackListener() {
            @Override
            public void onCallBackCompleted(Msg msg) {
                mDialog.dismiss();
                if (msg != null && msg.getSuccess() && msg.getData() != null && msg.getData() instanceof List) {
                    mList = (List) msg.getData();
                    if (mList != null) {
                        tv_count.setText("推荐人数："+mList.size()+"人");
                        if (mAdapter == null) {
                            mAdapter = new RecommendListAdapter(RecommendActivity.this);
                            mAdapter.setDataList(mList);
                            lv_data_list.setAdapter(mAdapter);
                        } else {
                            mAdapter.setDataList(mList);
                            mAdapter.notifyDataSetChanged();
                        }
                    }
                }
            }

            @Override
            public void onCallBackError(Msg msg) {
                mDialog.cancel();
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                back();
                break;
            case R.id.tv_share:
                Intent intent=new Intent(this,ShareActivity.class);

                startActivity(intent);
                break;
        }
    }
}
